
package com.wobaby.util.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

// Referenced classes of package com.wobaby.util.xml:
//            XMLElement

public class XMLDocument
{

    public XMLDocument(String filename)
    {
        this(filename, null);
    }

    public XMLDocument(File file)
    {
        this(file, null);
    }

    public XMLDocument(InputStream stream)
    {
        this(stream, null);
    }

    public XMLDocument(String filename, String ofType)
    {
        this(new File(filename), ofType);
    }

    public XMLDocument(File file, String ofType)
    {
        m_document = null;
        m_documentType = null;
        m_coreElement = null;
        initialize(file, ofType);
    }

    public XMLDocument(InputStream stream, String ofType)
    {
        m_document = null;
        m_documentType = null;
        m_coreElement = null;
        initialize(stream, ofType);
    }

    private void initialize(File file, String ofType)
    {
        try
        {
            initialize(((InputStream) (new FileInputStream(file))), ofType);
        }
        catch(Exception e)
        {
            throw new RuntimeException(e.getMessage() + " for file " + file.getAbsolutePath());
        }
    }

    private void initialize(InputStream stream, String ofType)
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try
        {
            DocumentBuilder builder = factory.newDocumentBuilder();
            InputStreamReader inputStreamReader = new InputStreamReader(stream);
            InputSource inputSource = new InputSource(inputStreamReader);
            m_document = builder.parse(inputSource);
        }
        catch(Exception e)
        {
            throw new RuntimeException("XML document parsing failure: " + e.getMessage());
        }
        if(m_document.getDoctype() != null)
            m_documentType = m_document.getDoctype().getName();
        if(ofType != null && !m_documentType.equals(ofType))
            throw new RuntimeException("XML document expected to be of type " + ofType + ", is of type " + m_documentType);
        NodeList nodes = m_document.getChildNodes();
        int index = 0;
        do
        {
            if(index >= nodes.getLength())
                break;
            Node node = nodes.item(index);
            if(node.getNodeType() == 1)
            {
                m_coreElement = new XMLElement(node);
                break;
            }
            index++;
        } while(true);
        if(m_coreElement == null)
            throw new RuntimeException("XML document has no core element");
        else
            return;
    }

    public String getType()
    {
        return m_documentType;
    }

    public XMLElement getCoreElement()
    {
        return m_coreElement;
    }

    private Document m_document;
    private String m_documentType;
    private XMLElement m_coreElement;
}